package gui.main.form;

import gui.standard.form.StandardForm;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.tablemodel.FormDialogTableModel;

public class NextDialog extends JDialog {
	
	/*
	 * 
	 * - Konstruktor klase prima parenta tipa Frame kao prvi parametar i listu StandardForm-i kao drugi parametar
	 * - Dijalog se prikazuje sa ime_promenljive.setVisible(true);
	 * - Posle zatvaranja dijaloga sa "X", "Odustani" ili "Izaberi" se pomocu metode isValueChosen() moze proveriti da li je odabrano "Izaberi" ili "X"/"Odustani"
	 * 	 ako metoda isValueChosen() vraca true znaci da se dijalog zatvorio sa "Izaberi", u suprotnom je dijalog zatvoren sa "X" ili "Odustani"
	 * - Index selektovane forme se posle zatvaranja dijaloga, ako je on zatvoren sa "Izaberi", preuzima pomocu metode getSelectedIndex()
	 * - Ako ni jedan element tabele nije bio selektovan getSelectedIndex() vraca vrednost -1
	 */

	private static final long serialVersionUID = 1L;
	private int selectedIndex = -1;
	private boolean chosen = false;
	private JButton btnOK;
	private JButton btnCancel;
	private JTable table;

	public NextDialog(Frame parent, ArrayList<StandardForm> toDisplay) {
		super(parent, "Izaberi formu", true);
		
		getContentPane().setLayout(new BorderLayout());
		JPanel tablePanel = new JPanel();
		tablePanel.setLayout(new BorderLayout());
		JPanel buttonPanel = new JPanel();
		
		String[] columnNames = {"Forme"};
		
		Object[][] data;
		
		if(toDisplay != null){
		
			data = new Object[toDisplay.size()][1];
			
			int i = 0;
			for(StandardForm sf : toDisplay){
				data[i][0] = sf;
				i++;
			}
			
		}else{
			data = new Object[0][0];
			
		}
		
		FormDialogTableModel tm = new FormDialogTableModel(data, columnNames);
		
		table = new JTable(tm);
		
		table.setRowSelectionAllowed(false);
		
		if(table.getRowCount() > 0){
			table.setRowSelectionInterval(0, 0);
		}
		
		JScrollPane scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		
		tablePanel.add(scrollPane, BorderLayout.CENTER);

		btnOK = new JButton("Izaberi");

		this.getRootPane().setDefaultButton(btnOK);

		btnOK.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				selectedIndex = table.getSelectedRow();
				chosen = true;
				NextDialog.this.setVisible(false);
			}
		});
		btnCancel = new JButton("Odustani");
		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				chosen = false;
				NextDialog.this.setVisible(false);
			}
		});
		buttonPanel.add(btnOK);
		buttonPanel.add(btnCancel);

		getContentPane().add(tablePanel, BorderLayout.CENTER);
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);

		pack();
		setResizable(false);
		setLocationRelativeTo(parent);
		
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				chosen = false;
				
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

	}

	public int getSelectedIndex() {
		return selectedIndex;
	}
	
	public boolean isValueChosen() {
		return chosen;
	}
	
}
